﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AForge;
using AForge.Math;

namespace ZenInterface
{
    enum digitalFilterState
    {
        Ready = 0,
        Filtering = 1
    }
    public partial class digitalFilterClass : Form
    {
        Properties.Settings settings;
        float prevUpdateRate = 0;
        int state = (int)digitalFilterState.Ready;
        Complex[] complexArray0;
        Complex[] complexArray1;
        Complex[] complexArray2;
        Complex[] complexArray3;

        bool[] blockArray0;
        bool[] blockArray1;
        bool[] blockArray2;
        bool[] blockArray3;

        Double[] gainArray0;
        Double[] gainArray1;
        Double[] gainArray2;
        Double[] gainArray3;

        Double[] phaseArray0;
        Double[] phaseArray1;
        Double[] phaseArray2;
        Double[] phaseArray3;

        DatabaseZenDataSet zenDataSet;

        #region functions
        public digitalFilterClass(Properties.Settings inSettings, DatabaseZenDataSet inDataSet)
        {
            InitializeComponent();

            settings = inSettings;
            zenDataSet = inDataSet;

            complexArray0 = new Complex[settings.applicationDataBufferSize];
            complexArray1 = new Complex[settings.applicationDataBufferSize];
            complexArray2 = new Complex[settings.applicationDataBufferSize];
            complexArray3 = new Complex[settings.applicationDataBufferSize];

            blockArray0 = new bool[settings.applicationDataBufferSize];
            blockArray1 = new bool[settings.applicationDataBufferSize];
            blockArray2 = new bool[settings.applicationDataBufferSize];
            blockArray3 = new bool[settings.applicationDataBufferSize];

            gainArray0 = new Double[settings.applicationDataBufferSize];
            gainArray1 = new Double[settings.applicationDataBufferSize];
            gainArray2 = new Double[settings.applicationDataBufferSize];
            gainArray3 = new Double[settings.applicationDataBufferSize];

            phaseArray0 = new Double[settings.applicationDataBufferSize];
            phaseArray1 = new Double[settings.applicationDataBufferSize];
            phaseArray2 = new Double[settings.applicationDataBufferSize];
            phaseArray3 = new Double[settings.applicationDataBufferSize];
 /*           
            DataRow dataRow;
            zenDataSet.DataTableDigitalFilter0.Clear();
            zenDataSet.DataTableDigitalFilter1.Clear();
            zenDataSet.DataTableDigitalFilter2.Clear();
            zenDataSet.DataTableDigitalFilter3.Clear();

            int i = 0;
            for (; i < settings.applicationDataBufferSize; i++)
            {
                dataRow = zenDataSet.DataTableDigitalFilter0.Rows[i];
                dataRow[1] = (double)(((int)dataRow[0]) * (settings.userSerialUpdateRate/settings.applicationDataBufferSize));
                dataRow[2] = 1;
                dataRow[3] = 0;

                dataRow = zenDataSet.DataTableDigitalFilter1.Rows[i];
                dataRow[1] = (double)(((int)dataRow[0]) * (settings.userSerialUpdateRate / settings.applicationDataBufferSize));
                dataRow[2] = 1;
                dataRow[3] = 0;

                dataRow = zenDataSet.DataTableDigitalFilter2.Rows[i];
                dataRow[1] = (double)(((int)dataRow[0]) * (settings.userSerialUpdateRate / settings.applicationDataBufferSize));
                dataRow[2] = 1;
                dataRow[3] = 0;

                dataRow = zenDataSet.DataTableDigitalFilter3.Rows[i];
                dataRow[1] = (double)(((int)dataRow[0]) * (settings.userSerialUpdateRate / settings.applicationDataBufferSize));
                dataRow[2] = 1;
                dataRow[3] = 0;
            }
*/

            changeUpdateRate();
            prevUpdateRate = settings.userSerialUpdateRate;

            //setup the data grids
            dataFilterDataGridView0.DataSource = zenDataSet;
            dataFilterDataGridView0.DataMember = "DataTableDigitalFilter0";

            dataFilterDataGridView1.DataSource = zenDataSet;
            dataFilterDataGridView1.DataMember = "DataTableDigitalFilter1";

            dataFilterDataGridView2.DataSource = zenDataSet;
            dataFilterDataGridView2.DataMember = "DataTableDigitalFilter2";

            dataFilterDataGridView3.DataSource = zenDataSet;
            dataFilterDataGridView3.DataMember = "DataTableDigitalFilter3";
        }
        public void applyDigitalFilter()
        {
            DataTable inDataTable = zenDataSet.DataTableChannels;
            DataTable outDataTable = zenDataSet.DataTableFilterChannels;

            DataRow dataRow;
            //move the data to a the complex values
           
            int i = 0;
            for (; i < settings.applicationDataBufferSize; i++)
            {
                dataRow = inDataTable.Rows[i];

                complexArray0[i].Re = (int)dataRow[0];
                complexArray1[i].Re = (int)dataRow[1];
                complexArray2[i].Re = (int)dataRow[2];
                complexArray3[i].Re = (int)dataRow[3];
            }

            //change data to the frequency domain
            FourierTransform.FFT(complexArray0, FourierTransform.Direction.Forward);
            FourierTransform.FFT(complexArray1, FourierTransform.Direction.Forward);
            FourierTransform.FFT(complexArray2, FourierTransform.Direction.Forward);
            FourierTransform.FFT(complexArray3, FourierTransform.Direction.Forward);

            //apply the filter
            i = 0;
            for (; i < settings.applicationDataBufferSize; i++)
            {

                //channel0
                dataRow = zenDataSet.DataTableDigitalFilter0.Rows[i];
                //if the frequency is enabled
                if (!(bool)zenDataSet.DataTableDigitalFilter0.Rows[i].ItemArray[4])
                {
                    //start adjusting things

                    //gain
                    complexArray0[i].Re = (((double)zenDataSet.DataTableDigitalFilter0.Rows[i].ItemArray[2]) * ((double)complexArray0[i].Magnitude));
                    complexArray0[i].Im = (((double)zenDataSet.DataTableDigitalFilter0.Rows[i].ItemArray[3]) * ((double)complexArray0[i].Magnitude));//+ ((double)complexArray0[i].Phase));
                }
                //block the frequency
                else
                {
                    complexArray0[i].Re = 0;
                    complexArray0[i].Im = 0;
                }

                dataRow[5] = complexArray0[i].Magnitude;
                dataRow[6] = complexArray0[i].Phase;


                //channel1
                dataRow = zenDataSet.DataTableDigitalFilter1.Rows[i];
                //if the frequency is enabled
                if (!(bool)zenDataSet.DataTableDigitalFilter1.Rows[i].ItemArray[4])
                {
                    //start adjusting things

                    //gain
                    complexArray1[i].Re = (((double)zenDataSet.DataTableDigitalFilter1.Rows[i].ItemArray[2]) * ((double)complexArray1[i].Magnitude));
                    complexArray1[i].Im = (((double)zenDataSet.DataTableDigitalFilter1.Rows[i].ItemArray[3]) * ((double)complexArray1[i].Magnitude));//+ ((double)complexArray1[i].Phase));
                }
                //block the frequency
                else
                {
                    complexArray1[i].Re = 0;
                    complexArray1[i].Im = 0;
                }
                dataRow[5] = complexArray1[i].Magnitude;
                dataRow[6] = complexArray1[i].Phase;
                

                //channel2
                dataRow = zenDataSet.DataTableDigitalFilter2.Rows[i];
                //if the frequency is enabled
                if (!(bool)zenDataSet.DataTableDigitalFilter2.Rows[i].ItemArray[4])
                {
                    //start adjusting things

                    //gain
                    complexArray2[i].Re = (((double)zenDataSet.DataTableDigitalFilter2.Rows[i].ItemArray[2]) * ((double)complexArray2[i].Magnitude));
                    complexArray2[i].Im = (((double)zenDataSet.DataTableDigitalFilter2.Rows[i].ItemArray[3]) * ((double)complexArray2[i].Magnitude));//+ ((double)complexArray2[i].Phase));
                }
                //block the frequency
                else
                {
                    complexArray2[i].Re = 0;
                    complexArray2[i].Im = 0;
                }
                dataRow[5] = complexArray2[i].Magnitude;
                dataRow[6] = complexArray2[i].Phase;
                
                //channel3
                dataRow = zenDataSet.DataTableDigitalFilter3.Rows[i];
                //if the frequency is enabled
                if (!(bool)zenDataSet.DataTableDigitalFilter3.Rows[i].ItemArray[4])
                {
                    //start adjusting things

                    //gain
                    complexArray3[i].Re = (((double)zenDataSet.DataTableDigitalFilter3.Rows[i].ItemArray[2]) * ((double)complexArray3[i].Magnitude));
                    complexArray3[i].Im = (((double)zenDataSet.DataTableDigitalFilter3.Rows[i].ItemArray[3]) * ((double)complexArray3[i].Magnitude));//+ ((double)complexArray3[i].Phase));
                }
                //block the frequency
                else
                {
                    complexArray3[i].Re = 0;
                    complexArray3[i].Im = 0;
                }
                dataRow[5] = complexArray3[i].Magnitude;
                dataRow[6] = complexArray3[i].Phase;
            }

            //change the data back
            FourierTransform.FFT(complexArray0, FourierTransform.Direction.Backward);
            FourierTransform.FFT(complexArray1, FourierTransform.Direction.Backward);
            FourierTransform.FFT(complexArray2, FourierTransform.Direction.Backward);
            FourierTransform.FFT(complexArray3, FourierTransform.Direction.Backward);



            //put data back into the data
            i = zenDataSet.DataTableFilterChannels.Rows.Count;
            for (; i < settings.applicationDataBufferSize; i++)
            {
                zenDataSet.DataTableFilterChannels.Rows.Add();
            }

            i = 0;
            for (; i < settings.applicationDataBufferSize; i++)
            {

                dataRow = outDataTable.Rows[i];

                dataRow[0] = complexArray0[i].Magnitude;
                dataRow[1] = complexArray1[i].Magnitude;
                dataRow[2] = complexArray2[i].Magnitude;
                dataRow[3] = complexArray3[i].Magnitude;
            }

            //initialize combo boxes
            //comboBoxChannel0LowFrequency.Enabled = false;
            //comboBoxChannel0HighFrequency.Enabled = false;


        }
        public void changeUpdateRate()
        {
            //update all the comboboxes
            DataRow dataRow;

            comboBoxChannel0LowFrequency.Items.Clear();
            comboBoxChannel0HighFrequency.Items.Clear();

            for (int i = 0; i < zenDataSet.DataTableFilterChannels.Rows.Count; i++)
            {
                //channel 0
                dataRow = zenDataSet.DataTableDigitalFilter0.Rows[i];
                dataRow[1] = (double)(((int)dataRow[0]) * ((double)(((double)settings.userSerialUpdateRate) / ((double)settings.applicationDataBufferSize))));

                comboBoxChannel0LowFrequency.Items.Add(dataRow[1].ToString());
                comboBoxChannel0LowFrequency.SelectedIndex = 0;
                comboBoxChannel0HighFrequency.Items.Add(dataRow[1].ToString());
                comboBoxChannel0HighFrequency.SelectedIndex = 0;
                
                //channel 1
                dataRow = zenDataSet.DataTableDigitalFilter1.Rows[i];
                dataRow[1] = (double)(((int)dataRow[0]) * ((double)(((double)settings.userSerialUpdateRate) / ((double)settings.applicationDataBufferSize))));

                comboBoxChannel1LowFrequency.Items.Add(dataRow[1].ToString());
                comboBoxChannel1LowFrequency.SelectedIndex = 0;
                comboBoxChannel1HighFrequency.Items.Add(dataRow[1].ToString());
                comboBoxChannel1HighFrequency.SelectedIndex = 0;

                //channel 2
                dataRow = zenDataSet.DataTableDigitalFilter2.Rows[i];
                dataRow[1] = (double)(((int)dataRow[0]) * ((double)(((double)settings.userSerialUpdateRate) / ((double)settings.applicationDataBufferSize))));

                comboBoxChannel2LowFrequency.Items.Add(dataRow[1].ToString());
                comboBoxChannel2LowFrequency.SelectedIndex = 0;
                comboBoxChannel2HighFrequency.Items.Add(dataRow[1].ToString());
                comboBoxChannel2HighFrequency.SelectedIndex = 0;

                //channel 3
                dataRow = zenDataSet.DataTableDigitalFilter3.Rows[i];
                dataRow[1] = (double)(((int)dataRow[0]) * ((double)(((double)settings.userSerialUpdateRate) / ((double)settings.applicationDataBufferSize))));

                comboBoxChannel3LowFrequency.Items.Add(dataRow[1].ToString());
                comboBoxChannel3LowFrequency.SelectedIndex = 0;
                comboBoxChannel3HighFrequency.Items.Add(dataRow[1].ToString());
                comboBoxChannel3HighFrequency.SelectedIndex = 0;
            }
        }
        public int update()
        {
            if (prevUpdateRate != settings.userSerialUpdateRate)
            {
                changeUpdateRate();
                prevUpdateRate = settings.userSerialUpdateRate;
            }

            return state;
        }
        public String stateTranslate(int inState)
        {
            String outputBuffer = String.Empty;

            switch (inState)
            {
                case ((int)digitalFilterState.Ready):
                    outputBuffer = Properties.Resources.stringNotConnected;// "Not Connected";
                    break;
                case ((int)digitalFilterState.Filtering):
                    outputBuffer = Properties.Resources.stringFiltering;
                    break;
                default:
                    outputBuffer = Properties.Resources.stringUnknown;// "???";
                    break;
            }

            return outputBuffer;

        }
        #endregion

        #region events
        private void buttonClearFilter_Click(object sender, EventArgs e)
        {
            DataRow dataRow;
            zenDataSet.DataTableDigitalFilter0.Clear();
            zenDataSet.DataTableDigitalFilter1.Clear();
            zenDataSet.DataTableDigitalFilter2.Clear();
            zenDataSet.DataTableDigitalFilter3.Clear();

            int i = 0;
            for (; i < settings.applicationDataBufferSize; i++)
            {
                dataRow = zenDataSet.DataTableDigitalFilter0.Rows.Add();
                dataRow[1] = (double)(((int)dataRow[0]) * ((double)(((double)settings.userSerialUpdateRate) / ((double)settings.applicationDataBufferSize))));
                dataRow[2] = 1;
                dataRow[3] = 0;

                dataRow = zenDataSet.DataTableDigitalFilter1.Rows.Add();
                dataRow[1] = (double)(((int)dataRow[0]) * ((double)(((double)settings.userSerialUpdateRate) / ((double)settings.applicationDataBufferSize))));
                dataRow[2] = 1;
                dataRow[3] = 0;

                dataRow = zenDataSet.DataTableDigitalFilter2.Rows.Add();
                dataRow[1] = (double)(((int)dataRow[0]) * ((double)(((double)settings.userSerialUpdateRate) / ((double)settings.applicationDataBufferSize))));
                dataRow[2] = 1;
                dataRow[3] = 0;

                dataRow = zenDataSet.DataTableDigitalFilter3.Rows.Add();
                dataRow[1] = (double)(((int)dataRow[0]) * ((double)(((double)settings.userSerialUpdateRate) / ((double)settings.applicationDataBufferSize))));
                dataRow[2] = 1;
                dataRow[3] = 0;
            } 
        }
        private void digitalFilterDataGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {

        }
        private void toolStripContainer1_ContentPanel_Load(object sender, EventArgs e)
        {

        }
        //channel 0
        private void checkBoxChannel0LowPass_CheckedChanged(object sender, EventArgs e)
        {

            //if the check is enabled then we need to get all the information for the combo box
            if (checkBoxChannel0LowPass.Checked)
            {
                comboBoxChannel0LowFrequency.Enabled = true;
                comboBoxChannel0HighFrequency.Enabled = false;
                checkBoxChannel0HighPass.Checked = false;
                checkBoxChannel0BandPass.Checked = false;
                //comboBoxChannel0HighFrequency.SelectedIndex = (settings.applicationDataBufferSize - 1);
            }
            else if (!checkBoxChannel0BandPass.Checked && !checkBoxChannel0HighPass.Checked && !checkBoxChannel0LowPass.Checked)
            {
                comboBoxChannel0HighFrequency.Enabled = false;
                comboBoxChannel0LowFrequency.Enabled = false;
            }
        }
        private void checkBoxChannel0HighPass_CheckedChanged(object sender, EventArgs e)
        {
            //if the check is enabled then we need to get all the information for the combo box
            if (checkBoxChannel0HighPass.Checked)
            {
                comboBoxChannel0LowFrequency.Enabled = false;
                comboBoxChannel0HighFrequency.Enabled = true;
                checkBoxChannel0LowPass.Checked = false;
                checkBoxChannel0BandPass.Checked = false;
            }
            else if (!checkBoxChannel0BandPass.Checked && !checkBoxChannel0HighPass.Checked && !checkBoxChannel0LowPass.Checked)
            {
                comboBoxChannel0HighFrequency.Enabled = false;
                comboBoxChannel0LowFrequency.Enabled = false;
            }
        }
        private void checkBoxChannel0BandPass_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxChannel0BandPass.Checked)
            {
                comboBoxChannel0LowFrequency.Enabled = true;
                comboBoxChannel0HighFrequency.Enabled = true;
                checkBoxChannel0LowPass.Checked = false;
                checkBoxChannel0HighPass.Checked = false;
            }
            else if (!checkBoxChannel0BandPass.Checked && !checkBoxChannel0HighPass.Checked && !checkBoxChannel0LowPass.Checked)
            {
                comboBoxChannel0HighFrequency.Enabled = false;
                comboBoxChannel0LowFrequency.Enabled = false;
            }
        }
        private void buttonChannel0ApplyFilter_Click(object sender, EventArgs e)
        {
            
            DataRow dataRow;

            //low pass
            
            if (checkBoxChannel0LowPass.Checked == true)
            {
                //go up to the low frequency signal, and disable every signal above that
                for (int i = comboBoxChannel0LowFrequency.SelectedIndex; i < settings.applicationDataBufferSize; i++)
                {
                    dataRow = zenDataSet.DataTableDigitalFilter0.Rows[i];
                    dataRow[4] = true;
                }
            }


            //high pass

            else if (checkBoxChannel0HighPass.Checked == true)
            {
                //go up to the high frequency signa, and disable every signal below that
                for (int i = comboBoxChannel0HighFrequency.SelectedIndex; i > 0; i--)
                {
                    dataRow = zenDataSet.DataTableDigitalFilter0.Rows[i];
                    dataRow[4] = true;
                }
            }
            
            //band pass
            else if (checkBoxChannel0BandPass.Checked == true)
            {
                //find the two frequency, and disable every signal below, and above that
                //go up to the high frequency signa, and disable every signal below that

                if (comboBoxChannel0HighFrequency.SelectedIndex > comboBoxChannel0LowFrequency.SelectedIndex)
                {
                    //bandpass
                    for (int i = comboBoxChannel0LowFrequency.SelectedIndex; i <= comboBoxChannel0HighFrequency.SelectedIndex; i++)
                    {
                        dataRow = zenDataSet.DataTableDigitalFilter0.Rows[i];
                        dataRow[4] = true;
                    }
                }
            }

            /*
            for (int i = 0; i < settings.applicationDataBufferSize; i++)
            {
             
                dataRow = zenDataSet.DataTableDigitalFilter0.Rows[i];
                gainArray0[i] = (Double)dataRow[2];
                phaseArray0[i] = (Double)dataRow[3];
                blockArray0[i] = (bool)dataRow[4];
            }
            */
            
        }
        private void buttonChannel0Clear_Click(object sender, EventArgs e)
        {
            //clear all the block signals
            DataRow dataRow;
            for (int i = 0; i < settings.applicationDataBufferSize; i++)
            {
                dataRow = zenDataSet.DataTableDigitalFilter0.Rows[i];
                dataRow[4] = false;
            }
        }
        //channel 1
        private void checkBoxChannel1LowPass_CheckedChanged(object sender, EventArgs e)
        {
            //if the check is enabled then we need to get all the information for the combo box
            if (checkBoxChannel1LowPass.Checked)
            {
                comboBoxChannel1LowFrequency.Enabled = true;
                comboBoxChannel1HighFrequency.Enabled = false;
                checkBoxChannel1HighPass.Checked = false;
                checkBoxChannel1BandPass.Checked = false;
                //comboBoxChannel0HighFrequency.SelectedIndex = (settings.applicationDataBufferSize - 1);
            }
            else if (!checkBoxChannel1BandPass.Checked && !checkBoxChannel1HighPass.Checked && !checkBoxChannel1LowPass.Checked)
            {
                comboBoxChannel1HighFrequency.Enabled = false;
                comboBoxChannel1LowFrequency.Enabled = false;
            }
        }
        private void checkBoxChannel1HighPass_CheckedChanged(object sender, EventArgs e)
        {
            //if the check is enabled then we need to get all the information for the combo box
            if (checkBoxChannel1HighPass.Checked)
            {
                comboBoxChannel1LowFrequency.Enabled = false;
                comboBoxChannel1HighFrequency.Enabled = true;
                checkBoxChannel1LowPass.Checked = false;
                checkBoxChannel1BandPass.Checked = false;
            }
            else if (!checkBoxChannel1BandPass.Checked && !checkBoxChannel1HighPass.Checked && !checkBoxChannel1LowPass.Checked)
            {
                comboBoxChannel1HighFrequency.Enabled = false;
                comboBoxChannel1LowFrequency.Enabled = false;
            }
        }
        private void checkBoxChannel1BandPass_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxChannel1BandPass.Checked)
            {
                comboBoxChannel1LowFrequency.Enabled = true;
                comboBoxChannel1HighFrequency.Enabled = true;
                checkBoxChannel1LowPass.Checked = false;
                checkBoxChannel1HighPass.Checked = false;
            }
            else if (!checkBoxChannel1BandPass.Checked && !checkBoxChannel1HighPass.Checked && !checkBoxChannel1LowPass.Checked)
            {
                comboBoxChannel1HighFrequency.Enabled = false;
                comboBoxChannel1LowFrequency.Enabled = false;
            }
        }
        private void buttonChannel1ApplyFilter_Click(object sender, EventArgs e)
        {

            DataRow dataRow;

            //low pass

            if (checkBoxChannel1LowPass.Checked == true)
            {
                //go up to the low frequency signal, and disable every signal above that
                for (int i = comboBoxChannel1LowFrequency.SelectedIndex; i < settings.applicationDataBufferSize; i++)
                {
                    dataRow = zenDataSet.DataTableDigitalFilter1.Rows[i];
                    dataRow[4] = true;
                }
            }


            //high pass

            if (checkBoxChannel1HighPass.Checked == true)
            {
                //go up to the high frequency signa, and disable every signal below that
                for (int i = comboBoxChannel1HighFrequency.SelectedIndex; i > 0; i--)
                {
                    dataRow = zenDataSet.DataTableDigitalFilter1.Rows[i];
                    dataRow[4] = true;
                }
            }

            //band pass
            else if (checkBoxChannel1BandPass.Checked == true)
            {
                //find the two frequency, and disable every signal below, and above that
                //go up to the high frequency signa, and disable every signal below that

                if (comboBoxChannel1HighFrequency.SelectedIndex > comboBoxChannel1LowFrequency.SelectedIndex)
                {
                    //bandpass
                    for (int i = comboBoxChannel1LowFrequency.SelectedIndex; i <= comboBoxChannel1HighFrequency.SelectedIndex; i++)
                    {
                        dataRow = zenDataSet.DataTableDigitalFilter1.Rows[i];
                        dataRow[4] = true;
                    }
                }
            }
        }
        private void buttonChannel1Clear_Click(object sender, EventArgs e)
        {
            //clear all the block signals
            DataRow dataRow;
            for (int i = 0; i < settings.applicationDataBufferSize; i++)
            {
                dataRow = zenDataSet.DataTableDigitalFilter1.Rows[i];
                dataRow[4] = false;
            }
        }
        //channel 2
        private void checkBoxChannel2LowPass_CheckedChanged(object sender, EventArgs e)
        {
            //if the check is enabled then we need to get all the information for the combo box
            if (checkBoxChannel2LowPass.Checked)
            {
                comboBoxChannel2LowFrequency.Enabled = true;
                comboBoxChannel2HighFrequency.Enabled = false;
                checkBoxChannel2HighPass.Checked = false;
                checkBoxChannel2BandPass.Checked = false;
                //comboBoxChannel2HighFrequency.SelectedIndex = (settings.applicationDataBufferSize - 1);
            }
            else if (!checkBoxChannel2BandPass.Checked && !checkBoxChannel2HighPass.Checked && !checkBoxChannel2LowPass.Checked)
            {
                comboBoxChannel2HighFrequency.Enabled = false;
                comboBoxChannel2LowFrequency.Enabled = false;
            }
        }
        private void checkBoxChannel2HighPass_CheckedChanged(object sender, EventArgs e)
        {
            //if the check is enabled then we need to get all the information for the combo box
            if (checkBoxChannel2HighPass.Checked)
            {
                comboBoxChannel2LowFrequency.Enabled = false;
                comboBoxChannel2HighFrequency.Enabled = true;
                checkBoxChannel2LowPass.Checked = false;
                checkBoxChannel2BandPass.Checked = false;
            }
            else if (!checkBoxChannel2BandPass.Checked && !checkBoxChannel2HighPass.Checked && !checkBoxChannel2LowPass.Checked)
            {
                comboBoxChannel2HighFrequency.Enabled = false;
                comboBoxChannel2LowFrequency.Enabled = false;
            }
        }
        private void checkBoxChannel2BandPass_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxChannel2BandPass.Checked)
            {
                comboBoxChannel2LowFrequency.Enabled = true;
                comboBoxChannel2HighFrequency.Enabled = true;
                checkBoxChannel2LowPass.Checked = false;
                checkBoxChannel2HighPass.Checked = false;
            }
            else if (!checkBoxChannel2BandPass.Checked && !checkBoxChannel2HighPass.Checked && !checkBoxChannel2LowPass.Checked)
            {
                comboBoxChannel2HighFrequency.Enabled = false;
                comboBoxChannel2LowFrequency.Enabled = false;
            }
        }
        private void buttonChannel2ApplyFilter_Click(object sender, EventArgs e)
        {

            DataRow dataRow;

            //low pass

            if (checkBoxChannel2LowPass.Checked == true)
            {
                //go up to the low frequency signal, and disable every signal above that
                for (int i = comboBoxChannel2LowFrequency.SelectedIndex; i < settings.applicationDataBufferSize; i++)
                {
                    dataRow = zenDataSet.DataTableDigitalFilter2.Rows[i];
                    dataRow[4] = true;
                }
            }


            //high pass

            if (checkBoxChannel2HighPass.Checked == true)
            {
                //go up to the high frequency signa, and disable every signal below that
                for (int i = comboBoxChannel2HighFrequency.SelectedIndex; i > 0; i--)
                {
                    dataRow = zenDataSet.DataTableDigitalFilter2.Rows[i];
                    dataRow[4] = true;
                }
            }

            //band pass
            else if (checkBoxChannel2BandPass.Checked == true)
            {
                //find the two frequency, and disable every signal below, and above that
                //go up to the high frequency signa, and disable every signal below that

                if (comboBoxChannel2HighFrequency.SelectedIndex > comboBoxChannel2LowFrequency.SelectedIndex)
                {
                    //bandpass
                    for (int i = comboBoxChannel2LowFrequency.SelectedIndex; i <= comboBoxChannel2HighFrequency.SelectedIndex; i++)
                    {
                        dataRow = zenDataSet.DataTableDigitalFilter2.Rows[i];
                        dataRow[4] = true;
                    }
                }
            }
        }
        private void buttonChannel2Clear_Click(object sender, EventArgs e)
        {
            //clear all the block signals
            DataRow dataRow;
            for (int i = 0; i < settings.applicationDataBufferSize; i++)
            {
                dataRow = zenDataSet.DataTableDigitalFilter2.Rows[i];
                dataRow[4] = false;
            }
        }
        //channel 3
        private void checkBoxChannel3LowPass_CheckedChanged(object sender, EventArgs e)
        {
            //if the check is enabled then we need to get all the information for the combo box
            if (checkBoxChannel3LowPass.Checked)
            {
                comboBoxChannel3LowFrequency.Enabled = true;
                comboBoxChannel3HighFrequency.Enabled = false;
                checkBoxChannel3HighPass.Checked = false;
                checkBoxChannel3BandPass.Checked = false;
                //comboBoxChannel3HighFrequency.SelectedIndex = (settings.applicationDataBufferSize - 1);
            }
            else if (!checkBoxChannel3BandPass.Checked && !checkBoxChannel3HighPass.Checked && !checkBoxChannel3LowPass.Checked)
            {
                comboBoxChannel3HighFrequency.Enabled = false;
                comboBoxChannel3LowFrequency.Enabled = false;
            }
        }
        private void checkBoxChannel3HighPass_CheckedChanged(object sender, EventArgs e)
        {
            //if the check is enabled then we need to get all the information for the combo box
            if (checkBoxChannel3HighPass.Checked)
            {
                comboBoxChannel3LowFrequency.Enabled = false;
                comboBoxChannel3HighFrequency.Enabled = true;
                checkBoxChannel3LowPass.Checked = false;
                checkBoxChannel3BandPass.Checked = false;
            }
            else if (!checkBoxChannel3BandPass.Checked && !checkBoxChannel3HighPass.Checked && !checkBoxChannel3LowPass.Checked)
            {
                comboBoxChannel3HighFrequency.Enabled = false;
                comboBoxChannel3LowFrequency.Enabled = false;
            }
        }
        private void checkBoxChannel3BandPass_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxChannel3BandPass.Checked)
            {
                comboBoxChannel3LowFrequency.Enabled = true;
                comboBoxChannel3HighFrequency.Enabled = true;
                checkBoxChannel3LowPass.Checked = false;
                checkBoxChannel3HighPass.Checked = false;
            }
            else if (!checkBoxChannel3BandPass.Checked && !checkBoxChannel3HighPass.Checked && !checkBoxChannel3LowPass.Checked)
            {
                comboBoxChannel3HighFrequency.Enabled = false;
                comboBoxChannel3LowFrequency.Enabled = false;
            }
        }
        private void buttonChannel3ApplyFilter_Click(object sender, EventArgs e)
        {
            DataRow dataRow;

            //low pass

            if (checkBoxChannel3LowPass.Checked == true)
            {
                //go up to the low frequency signal, and disable every signal above that
                for (int i = comboBoxChannel3LowFrequency.SelectedIndex; i < settings.applicationDataBufferSize; i++)
                {
                    dataRow = zenDataSet.DataTableDigitalFilter3.Rows[i];
                    dataRow[4] = true;
                }
            }


            //high pass

            if (checkBoxChannel3HighPass.Checked == true)
            {
                //go up to the high frequency signa, and disable every signal below that
                for (int i = comboBoxChannel3HighFrequency.SelectedIndex; i > 0; i--)
                {
                    dataRow = zenDataSet.DataTableDigitalFilter3.Rows[i];
                    dataRow[4] = true;
                }
            }

            //band pass
            else if (checkBoxChannel3BandPass.Checked == true)
            {
                //find the two frequency, and disable every signal below, and above that
                //go up to the high frequency signa, and disable every signal below that

                if (comboBoxChannel3HighFrequency.SelectedIndex > comboBoxChannel3LowFrequency.SelectedIndex)
                {
                    //bandpass
                    for (int i = comboBoxChannel3LowFrequency.SelectedIndex; i <= comboBoxChannel3HighFrequency.SelectedIndex; i++)
                    {
                        dataRow = zenDataSet.DataTableDigitalFilter3.Rows[i];
                        dataRow[4] = true;
                    }
                }
            }
        }
        private void buttonChannel3Clear_Click(object sender, EventArgs e)
        {
            //clear all the block signals
            DataRow dataRow;
            for (int i = 0; i < settings.applicationDataBufferSize; i++)
            {
                dataRow = zenDataSet.DataTableDigitalFilter3.Rows[i];
                dataRow[4] = false;
            }
        }

        private void digitalFilterClass_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                this.Hide();
            }
        }

        #endregion

    }
}
